package linklist;

public class MySingleList implements linklist{

    // 节点的内部类
    static class Node {
        public int val;
        public Node next;

        public Node(int val) {
            this.val = val;
        }
    }

    public Node head;

    // 头插法
    @Override
    public void addFirst(int data) {
        Node node = new Node(data);
        if (this.head == null) {
            this.head = node;
        } else {
            node.next = this.head;
            this.head = node;
        }

    }

    public void createList() {
        Node node1 = new Node(11);
        Node node2 = new Node(22);
        Node node3 = new Node(33);
        Node node4 = new Node(44);
        Node node5 = new Node(55);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        head = node1;
    }

    // 尾插法
    @Override
    public void addLast(int data) {
        Node node = new Node(data);
        Node cur = head;
        if (head == null) {
            this.head = node;
        } else {
           // 找到尾巴
            while(cur.next != null) {
                cur = cur.next;
            }

            cur.next = node;
        }

    }

    @Override
    public void addIndex(int index, int data) throws PosIllegality{
        if (index < 0 ||index > size()) {
            throw new PosIllegality("下标不合法");
        }

        if (index == 0) {
            addFirst(data);
            return;
        }

        if (index == size()) {
            addLast(data);
            return;
        }

        Node preNode = findPreNodeOfIndex(index);
        Node node = new Node(data);
        node.next = preNode.next;
        preNode.next = node;
    }

    private Node findPreNodeOfIndex(int index) {
        int count = 0;
        Node cur = head;
        while (count != index -1 ) {
            count++;
            cur = cur.next;
        }

        return cur;

    }

    @Override
    public boolean contains(int key) {
        Node cur = head;
        while (cur != null) {
            if (cur.val == key) return true;
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) throws ValIllegality{
        if (this.head == null) {
            return;
        }

        if (this.head.val == key) {
            this.head = this.head.next;
            return;
        }

        // 找到前驱
        Node preNode = findPreNodeOfVal(key);
        if(preNode == null) {
            throw new ValIllegality("没有找到数值");

        }
        // 删除
        Node del = preNode.next;
        preNode.next = del.next;

    }

    // 找到关键字key的前一个节点的地址
    private Node findPreNodeOfVal(int key) {
        Node cur = this.head;
        while (cur.next != null) {
            if(cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }

        return null;
    }

    @Override
    public void removeAllKey(int key) {
        if (this.head == null) {
            return;
        }
        Node preNode = this.head;
        Node cur = this.head.next;

        while (cur != null) {
            if (cur.val == key) {
                preNode.next = cur.next;
            } else {
                preNode = cur;
            }
            cur = cur.next;
        }

        if (this.head.val == key) {
            this.head = this.head.next;
        }


    }

    @Override
    public int size() {
       int count = 0;
       Node cur = head;
       while (cur != null) {
           count++;
           cur = cur.next;
       }
        return count;
    }

    @Override
    public void clear() {
        Node cur = this.head;
        while (cur != null) {
            Node curNext = cur.next;
            cur.next = null;
            cur = curNext;
        }

        head = null;

    }

    @Override
    public void display() {
        Node cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
}
